'''
creditscreen
Author: Alex Foran
'''

import pygame,gameroot,loaders,pane,utility,titlescreen

'''
A full-screen pane to show credits from an XML file.
'''
class CreditScreen (pane.Pane):
	'''
	Constructor.
	
	Params:
	root - A reference to the current gameroot object.
	'''
	def __init__(self,root):
		# super call
		pane.Pane.__init__(self,root)
		#defaults to showing nothing
		self.credits=[]
		self.currentscroll=0
		self.height=0
		#one-time math calculations
		self.h36=self.root.font36.size("!g")[1]
		self.h24=self.root.font24.size("!g")[1]
		#load the data from the XML
		self.loaddata()
		#play music - you're the best.
		self.root.audiosys.set_bgm(loaders.load_sound(gameroot.respath+"music/credits.ogg"))
		#load silly, superfluous image
		self.bgimage=loaders.load_image(gameroot.respath+"credits/credits.png",-1)
		
	'''
	Loads data from resources/credits/credits.xml.
	'''
	def loaddata(self):
		#read and parse XML
		xml=utility.loadXML(gameroot.respath+"credits/credits.xml")
		#error handling
		if xml is None:
			#put in default data
			nonegroup=CreditScreenGroup("Error loading credits file")
			nonegroup.children.append("Should probably look into that.")
			self.credits.append(nonegroup)
		#no errors
		else:
			#find all groups
			groups=xml.getElementsByTagName("group")
			for group in groups:
				#populate all groups
				g=CreditScreenGroup(group.attributes["name"].value)
				values=group.getElementsByTagName("credit")
				#add listings
				for value in values:
					g.children.append(value.attributes["name"].value)
				self.credits.append(g)
		#calculate total height - don't want to do this every frame
		totalchildren=0
		for group in self.credits:
			totalchildren+=len(group.children)
		self.height=self.h36*len(self.credits)+self.h24*totalchildren+3*self.h24*len(self.credits)
	
	'''
	Handles text scrolling. Pressing cancel will exit the credits.
	When the credits are done, the title screen is displayed.
	'''
	def update(self):
		#check soft exit
		if self.root.monitor.getjustpressed(self.root.prefs["key_cancel"]):
			self.done=True
		else:
			#increment scroll - at normal rate this is fine, if not slightly too fast.
			self.currentscroll+=1
			#if scroll has gone too far, we're done.
			if self.currentscroll>self.height+self.root.prefs["resolution"][1]:
				self.done=True
		#if we're done, replace self with title screen.
		if self.done:
			self.root.pane=titlescreen.TitleScreen(self.root)
	
	'''
	Redraws self.
	'''
	def repaint(self):
		surface=pygame.display.get_surface()
		#draw background
		surface.fill(pygame.color.Color("black"))
		#draw background image
		surface.blit(self.bgimage,(self.root.prefs["resolution"][0]-self.bgimage.get_width(),self.root.prefs["resolution"][1]-self.bgimage.get_height()))
		#draw all the credits
		offset=0
		for group in self.credits:
			surface.blit(self.root.font36.render(group.name,True,pygame.color.Color("white")),(20,self.root.prefs["resolution"][1]-self.currentscroll+offset))
			offset+=self.h36
			for credit in group.children:
				surface.blit(self.root.font24.render(credit,True,pygame.color.Color("white")),(60,self.root.prefs["resolution"][1]-self.currentscroll+offset))
				offset+=self.h24
			offset+=3*self.h24

'''
Handy subclass to hold a group name and an array of child strings.
'''
class CreditScreenGroup:
	def __init__(self,name):
		self.name=name
		self.children=[]